<template>
  <div>
    <!-- 顶部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-weight: 16">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/sys-admin/product/attribute' }">属性管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增属性</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <!-- 添加数据的表单 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="属性模板" prop="templateId">
        <el-select v-model="ruleForm.templateId" placeholder="请选择属性模板" style="width: 350px">
          <el-option
              v-for="item in attributeTemplateListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <template>
          <el-radio-group v-model="ruleForm.type">
            <el-radio :label="0">非销售类型
              <el-tooltip class="item" effect="dark" content="对商品销售不产生影响，但应补充说明的属性，例如：手机的产地"
                          placement="top">
                <i class="el-icon-info" style="color: #aaa"></i>
              </el-tooltip>
            </el-radio>
            <el-radio :label="1">销售类型
              <el-tooltip class="item" effect="dark"
                          content="必须确定属性的值才可以进行商品销售的属性，例如：手机的内存容量" placement="top">
                <i class="el-icon-info" style="color: #aaa"></i>
              </el-tooltip>
            </el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="输入类型" prop="inputType">
        <el-radio-group v-model="ruleForm.inputType">
          <el-radio :label="0">手动录入</el-radio>
          <el-radio :label="1">单选</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备选值列表" prop="valueList">
        <el-input v-model="ruleForm.valueList" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="ruleForm.unit" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="gotoList">属性列表</el-button>

      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 下拉的属性模板菜单数据
      attributeTemplateListOptions: [],
      // 属性添加数据的表单
      ruleForm: {
        templateId: '',
        name: '',
        description: '',
        type: '',
        inputType: '',
        valueList: '',
        unit: '',
        sort: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        description: [
          {required: false, message: '请输入简介', trigger: 'blur'},
          {min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请输入属性类型', trigger: 'blur'},
        ],
        inputType: [
          {required: true, message: '请输入输入类型', trigger: 'blur'},
        ],
        valueList: [
          {required: false, message: '请输入备选值列表', trigger: 'blur'},
          {min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: false, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是0~99之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9180/attributes/add-new';
          console.log('url='+url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData='+formData);
          let localJwt = localStorage.getItem('localJwt');
          this.axios.create({headers:{'Authorization':localJwt}}).post(url,formData)
              .then((response)=>{
                let jsonResult = response.data;
                if (jsonResult.state==20000){
                  const h = this.$createElement;
                  this.$notify({
                    title: '标题名称',
                    message: h('i', {style: 'color: teal'}, '添加属性成功！')
                  });
                  this.resetForm(formName);
                }else {
                  this.$alert(jsonResult.message, '错误', {
                    confirmButtonText: '确定',
                    callback: action => {
                    }
                  });
                }
              })
        } else {
          let message = '表单信息不正确！';
          this.$alert(message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 返回列表
    gotoList(){
      this.$router.push('/sys-admin/product/attribute');
    },
    // 加载属性模板列表
    loadAttributeTemplateList() {
      let url = 'http://localhost:9180/attribute-template?queryType=all';
      console.log('url=' + url);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.attributeTemplateListOptions = jsonResult.data.list;
        }
      })
    }
  },
  mounted() {
    this.loadAttributeTemplateList();
  }
}
</script>